<?php

include_once F_SOURCE . 'group/group.model.class.php';


/**
 * 权限组管理Ctrl类
 * 
 * @author Dean
 *
 */
class groupCtrl extends Ctrl{
	
	/**
	 * 显示权限组列表
	 */
	public function show() {
		$view = new View();
		
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
		
		$groupModel = new groupModel();
		
		$number = $groupModel->countAll();
		
		$groupList = $groupModel->findAll(($pageIndex-1)*PageNum, PageNum);	
		
		$page = pageCount($number, $pageIndex, '?do=group.show'); 
		
		$data = array(
			'op_select' => array('group'=>"class=select"),
			'groupList' => $groupList,
			'page' => $page
		);
		
		$view->display($data, 'group_show.html');
	}
	
	/**
	 * 查询单个权限组
	 */
	public function info(){
		
		$view = new View();
		
		$id = $_GET['id'];
		
		$groupModel = new groupModel();
		$groupInfo = $groupModel->findOne($id);	
		
		if($groupInfo!=''){
			$resourceArray = unserialize($groupInfo->groupAction);
		}
		
		$data = array(
			'op' => 'update',
			'op_select' => array('group'=>"class=select"),
			'obj' => $groupInfo,
			'resourceArray' => $resourceArray
		);
		
		$view->display($data, 'group_view.html');
	}
	
	/**
	 * 跳转新增页面
	 */
	public function view(){
		$view = new View();

		$data = array(
			'op' => $_GET['op'],
			'op_select' => array('group'=>"class=select")
		);
		
		$view->display($data, 'group_view.html');
	}
	
	/**
	 * 新增权限组操作
	 * 
	 */
	public function add(){
		
		$groupname = $_POST['groupname'];
		$resource = $_POST['resource'];
		
		$data = array(
			'groupName' => $groupname,
			'groupAction' => serialize($resource),
			'createTime' => getNowTime(),
			'modifyTime' => getNowTime(),
			'createUser' => $_SESSION['news']['user']->id
		);

		$groupModel = new groupModel();
		$insertID = $groupModel->add($data);
			
		if($insertID > 0){
			$this->showMessage('?do=group.show', 'op_success');
		}else{
			$this->showMessage('?do=group.view&op=add', 'op_failure');
		}
	}
	
	/**
	 * 修改权限组信息操作，通过权限组编号
	 * 
	 */
	public function modify(){
		
		$id = $_POST['id'];
		$groupname = $_POST['groupname'];
		$resource = $_POST['resource'];
		
		$data = array(
			'groupName' => $groupname,
			'groupAction' => serialize($resource),
			'modifyTime' => getNowTime()
		);

		$where = array(
			'id' => $id
		);
		
		$groupModel = new groupModel();
		$number = $groupModel->modify($data, $where);
			
		if($number > 0){
			$this->showMessage('?do=group.show', 'op_success');
		}else{
			$this->showMessage('?do=group.view&op=add', 'op_failure');
		}
		
	}
	
	/**
	 * 删除权限组操作，通过权限组编号
	 * 
	 */
	public function delete(){
		
		$id = intval($_GET['id'])>0?intval($_GET['id']):0;
		
		if($id<1){
			$this->showMessage('?do=group.show', 'op_wrong_id');
		}
		
		$data = array(
			'id' => $id
		);
		
		$groupModel = new groupModel();
		$success = $groupModel->delete($data);
		
		if($success > 0){
			$this->showMessage('?do=group.show', 'op_success');
		}else{
			$this->showMessage('?do=group.show', 'op_failure');
		}
	}

}


?>